package com.xiaoyu.lanling.features.chat.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.ViewPager
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.view.FragmentAdapter
import com.xiaoyu.lanling.view.OnShowFragment
import com.xiaoyu.lanling.feature.view.ScaleCircleNavigator
import kotlinx.android.synthetic.main.gift_normal_fragment.*
import net.lucode.hackware.magicindicator.ViewPagerHelper

abstract class AbstractGiftFragment<GiftItem> : OnShowFragment() {

    companion object {
        const val GIFT_NUM_PER_PAGE = 8
    }

    abstract fun getLayoutID(): Int

    abstract fun createItemFragment(giftList: List<GiftItem>): OnShowFragment

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(getLayoutID(), container, false)
    }

    override fun onShow() {
    }

    fun generateFragments(giftList: List<GiftItem>): List<OnShowFragment> {
        val fragments = mutableListOf<OnShowFragment>()
        if (giftList.isEmpty()) {
            return fragments
        }

        val pageSize = ((giftList.size - 1) / GIFT_NUM_PER_PAGE) + 1 // start from 1
        var fromIndex = 0
        for (i in 1..pageSize) {
            val toIndex = minOf(i * GIFT_NUM_PER_PAGE, giftList.size)

            val subList = giftList.subList(fromIndex, toIndex)
            fromIndex += subList.size

            val fragment = createItemFragment(subList)
            fragments.add(fragment)
        }
        return fragments
    }

    fun initFragmentAdapter(fragments: List<OnShowFragment>) {
        val titles: MutableList<CharSequence> = ListUtil.convertListWithIndex(fragments) { index, _ -> index.toString() }
        val bannerCount = fragments.size

        val fragmentAdapter = FragmentAdapter(childFragmentManager, fragments, titles)
        view_pager.adapter = fragmentAdapter
        view_pager.offscreenPageLimit = bannerCount
        view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                fragmentAdapter.tryShowOnIndex(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        initMagicIndicator(bannerCount)
        view_pager.currentItem = 0
        fragmentAdapter.tryShowOnIndex(0)
    }

    private fun initMagicIndicator(pictureListSize: Int) {
        val scaleCircleNavigator = ScaleCircleNavigator(activity)
        scaleCircleNavigator.setCircleCount(pictureListSize)
        scaleCircleNavigator.setNormalCircleColor(AppContext.getColor(R.color.scale_circle_navigator_normal))
        scaleCircleNavigator.setSelectedCircleColor(AppContext.getColor(R.color.scale_circle_navigator_select))
        scaleCircleNavigator.setCircleClickListener { index -> view_pager.currentItem = index }
        magic_indicator.navigator = scaleCircleNavigator
        ViewPagerHelper.bind(magic_indicator, view_pager)
    }
}